package com.gc.web.controller.business;

import cn.hutool.core.date.DateUtil;
import com.gc.business.domain.ElevatorBrand;
import com.gc.business.domain.ElevatorModel;
import com.gc.business.domain.FaultManage;
import com.gc.business.service.IElevatorBrandService;
import com.gc.business.service.IElevatorModelService;
import com.gc.business.service.IFaultManageService;
import com.gc.common.annotation.Log;
import com.gc.common.base.Result;
import com.gc.common.enums.BusinessType;
import com.gc.common.support.Convert;
import com.gc.common.utils.poi.ExcelUtil;
import com.gc.framework.util.ShiroUtils;
import com.gc.framework.web.base.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 故障管理 控制层
 *
 * @author 刘锦腾
 * @date 2020-12-08
 */
@Slf4j
@Controller
@RequestMapping("/business/faultManage")
public class FaultManageController extends BaseController {
    private String prefix = "business/faultManage";

    private final IFaultManageService faultManageService;

    private final IElevatorModelService elevatorModelService;

    private final IElevatorBrandService elevatorBrandService;


    @Autowired
    public FaultManageController(IFaultManageService faultManageService,
                                 IElevatorModelService elevatorModelService,
                                 IElevatorBrandService elevatorBrandService) {
        this.faultManageService = faultManageService;
        this.elevatorModelService = elevatorModelService;
        this.elevatorBrandService = elevatorBrandService;
    }

    /**
     * 列表页
     */
    @RequiresPermissions("business:faultManage:view")
    @GetMapping
    public String faultManage() {
        return prefix + "/faultManage";
    }

    /**
     * 新增页
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap) {
        List<ElevatorBrand> elevatorBrandList = elevatorBrandService.list(new ElevatorBrand());
        modelMap.put("elevatorBrandList", elevatorBrandList);
        return prefix + "/add";
    }

    /**
     * 编辑页
     */
    @GetMapping("/edit/{faultId}")
    public String edit(@PathVariable("faultId") Long faultId, ModelMap modelMap) {
        FaultManage faultManage = faultManageService.getById(faultId);
        modelMap.put("faultManage", faultManage);
        List<ElevatorBrand> elevatorBrandList = elevatorBrandService.list(new ElevatorBrand());
        modelMap.put("elevatorBrandList", elevatorBrandList);
        modelMap.put("modelNames", faultManage.getModelName().split(","));
        return prefix + "/edit";
    }

    /**
     * 查询故障管理列表
     */
    @RequiresPermissions("business:faultManage:list")
    @PostMapping("/list")
    @ResponseBody
    public Result list(FaultManage faultManage) {
        startPage();
        List<FaultManage> list = faultManageService.list(faultManage);
        return Result.success(getDataTable(list));
    }

    /**
     * 新增保存故障管理
     */
    @RequiresPermissions("business:faultManage:add")
    @Log(title = "故障管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public Result addSave(FaultManage faultManage) {
        faultManage.setCreateBy(ShiroUtils.getLoginName());
        faultManage.setCreateTime(DateUtil.date());
        if (faultManage.getBrandId() == null || faultManage.getModelIds() == null) {
            return Result.failed("请选择电梯品牌或型号");
        }
        ElevatorBrand elevatorBrand = elevatorBrandService.getById(faultManage.getBrandId());
        String[] split = faultManage.getModelIds().split(",");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < split.length; i++) {
            ElevatorModel elevatorModel = elevatorModelService.getById(split[i]);
            if (elevatorModel == null) {
                continue;
            }
            if (i == split.length - 1) {
                sb.append(elevatorModel.getModelName());
            } else {
                sb.append(elevatorModel.getModelName() + ",");
            }
        }
        faultManage.setBrandName(elevatorBrand.getBrandName());
        faultManage.setModelName(sb.toString());
        faultManageService.save(faultManage);
        return Result.success();
    }

    /**
     * 修改保存故障管理
     */
    @RequiresPermissions("business:faultManage:edit")
    @Log(title = "故障管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public Result editSave(FaultManage faultManage) {
        faultManage.setCreateBy(ShiroUtils.getLoginName());
        faultManage.setCreateTime(DateUtil.date());
        if (faultManage.getBrandId() == null || faultManage.getModelIds() == null) {
            return Result.failed("请选择电梯品牌或型号");
        }
        ElevatorBrand elevatorBrand = elevatorBrandService.getById(faultManage.getBrandId());
        String[] split = faultManage.getModelIds().split(",");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < split.length; i++) {
            ElevatorModel elevatorModel = elevatorModelService.getById(split[i]);
            if (elevatorModel == null) {
                continue;
            }
            if (i == split.length - 1) {
                sb.append(elevatorModel.getModelName());
            } else {
                sb.append(elevatorModel.getModelName() + ",");
            }
        }
        faultManage.setBrandName(elevatorBrand.getBrandName());
        faultManage.setModelName(sb.toString());
        faultManageService.updateById(faultManage);
        return Result.success();
    }

    /**
     * 删除故障管理
     */
    @RequiresPermissions("business:faultManage:remove")
    @Log(title = "故障管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public Result remove(String ids) {
        Long[] longIds = Convert.toLongArray(ids);
        faultManageService.removeByIds(Arrays.asList(longIds));
        return Result.success();
    }

    /**
     * 导出故障管理列表
     */
    @RequiresPermissions("business:faultManage:export")
    @PostMapping("/export")
    @ResponseBody
    public Result export(FaultManage faultManage) {
        List<FaultManage> list = faultManageService.list(faultManage);
        ExcelUtil<FaultManage> util = new ExcelUtil<>(FaultManage.class);
        util.exportExcel(list, "faultManage");
        return Result.success();
    }

    /**
     * 故障管理详情
     */
    @RequiresPermissions("business:faultManage:detail")
    @GetMapping("/detail/{faultId}")
    public String passDetail(@PathVariable("faultId") Long faultId, ModelMap mmap) {
        FaultManage faultManage = faultManageService.getById(faultId);
        mmap.put("faultManage", faultManage);
        return prefix + "/detail";
    }
}
